Repository navigation
Always use LF line endings in scaffolded code - #39601
Open
ajcvickers wants to merge 1 commit into
Open
ajcvickers wants to merge 1 commit into
ajcvickers wants to merge 1 commit into
Conversation
The pre-processed T4 templates baked CRLF into their string literals, while WriteLine and the CSharpHelper fluent-chain/XML-comment output used Environment.NewLine, so scaffolded files had mixed line endings on Linux/macOS (and the output depended on the platform). Scaffolded code now always uses LF: - The templates and their pre-processed output use literal LF, and no longer call WriteLine (which uses the platform newline in the Mono template base). - CSharpHelper gets a protected virtual NewLine used by Fragment and XmlComment. Scaffolding uses ScaffoldingCSharpHelper (LF), substituted for the default ICSharpHelper for both CSharpModelGenerator and TextTemplatingModelGenerator. Migrations, snapshots and compiled models are unchanged. - The four template files are pinned to LF in .gitattributes/.editorconfig, and the shipped templates are patched with LF. Fixes dotnet#29948 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #29948
Code produced by
dotnet ef dbcontext scaffoldhad mixed line endings on non-Windows platforms: the pre-processed T4 templates baked CRLF into their string literals, whileWriteLineand theCSharpHelperfluent-chain/XML-comment output usedEnvironment.NewLine. Blank lines (fromWriteLine("")) were LF on Linux/macOS and everything else CRLF. This PR makes scaffolded code always use LF, on every platform, by emitting literal LF rather than parsing and replacing the output afterwards.Changes
CSharpDbContextGenerator/CSharpEntityTypeGenerator(.ttand generated.cs) use literal LF. The base-classEnvironment.NewLineuses are replaced, and the templates no longer callWriteLine("")(it is implemented by the Mono template base class for custom templates and uses the platform newline); they useWrite("\n")instead.CSharpHelper:Fragment(both overloads) andXmlCommentnow use aprotected virtual NewLine(defaultEnvironment.NewLine) instead ofIndentedStringBuilder. Output is identical under the default.ScaffoldingCSharpHelper: overridesNewLineto"\n".LfCSharpHelperServiceProvidersubstitutes it for the defaultICSharpHelperfor the templates, in bothCSharpModelGeneratorandTextTemplatingModelGenerator. A replaced or subclassedICSharpHelperis left alone. Migrations, snapshots and compiled models keep usingEnvironment.NewLine.eol=lfin.gitattributesand.editorconfig. This matters because the generated DbContext code contains a verbatim string (#warning ...) with a real line break whose value depends on the file's line endings. ThePatchTemplatetask inEFCore.Templatesnow writes LF, so the shipped.t4files are LF regardless of the build machine.Tests
CSharpModelGeneratorandTextTemplatingModelGenerator, with and without data annotations, and asserts there is no\r.CSharpHelpertests: exact LF output for the scaffolding helper, and a platform-independent test of theNewLinehook using a CRLF subclass.AssertFileContents(about 75 scaffolding tests) no longer ignores line-ending differences, andAssertContainsno longer converts the expected text toEnvironment.NewLine.Notes
.csfiles were edited to match what the pre-processor emits, rather than regenerated, to avoid unrelated churn from a different pre-processor version. Regenerating in Visual Studio will bringEnvironment.NewLineback in the generated base class; the new tests catch that on Windows..t4templates keep whatever line endings the template file has.CSharpRuntimeModelCodeGenerator(compiled models) and migrations.🤖 Generated with Claude Code